package com.zb.smbmsc.service.user;

import com.zb.smbmsc.dao.BaseDao;
import com.zb.smbmsc.dao.user.UserDao;
import com.zb.smbmsc.dao.user.UserDaoImpl;
import com.zb.smbmsc.pojo.Role;
import com.zb.smbmsc.pojo.User;
import org.junit.Test;

import java.awt.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{

    private UserDao userDao;

    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            user = userDao.getUser(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(connection, null, null);
        }

        return user;
    }

    @Override
    public boolean updatePwd(int userId, String userPassword) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = userDao.updatePwd(connection, userId, userPassword);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(connection, null, null);
        }
        return flag;
    }

    @Override
    public int selectUserCount(String userName, String userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();;
            count = userDao.selectUserCount(connection, userName, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(connection, null, null);
        }
        return count;
    }

    @Override
    public List<User> selectUserList(String userName, String userRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> users = null;

        try {
            connection = BaseDao.getConnection();
            users = userDao.selectUserList(connection, userName, userRole, currentPageNo, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(connection, null, null);
        }

        return users;
    }

    @Override
    public List<Role> getRoleList() {
        Connection connection = null;
        List<Role> roleList = null;
        try {
            connection = BaseDao.getConnection();
            roleList = userDao.getRoleList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(connection,null,null);
        }
        return roleList;
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        System.out.println(userService.getRoleList());
    }

}
